 package com.stardata.liangshisq.tcslmon.util;

 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.lang.Nullable;

 import java.util.HashMap;
 import java.util.Map;
 import java.util.Objects;

 public class R extends ResponseEntity<Map<String, Object>> {

   private static final long serialVersionUID = 1L;

   public R(@Nullable Map<String, Object> body, HttpStatus status) {
     super(body, status);
     put("errcode", 0);
   }

   public static R error() {
     return error("未知异常");
   }

   public static R error(String msg) {
     HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
     Map<String, Object> body = new HashMap<>();
     R r = new R(body, status);
     r.put("errcode", status.value());
     r.put("errmsg", msg);
     return r;
   }

   public static R custom(HttpStatus status, String msg) {
     Map<String, Object> body = new HashMap<>();
     R r = new R(body, status);
     r.put("errcode", status.value());
     r.put("errmsg", msg);
     return r;
   }


   public static R okay(String msg) {
     Map<String, Object> body = new HashMap<>();
     R r = new R(body, HttpStatus.OK);
     r.put("errmsg", msg);
     return r;
   }

   public static R okay(Map<String, Object> map) {
     Map<String, Object> body = new HashMap<>();
     R r = new R(body, HttpStatus.OK);
     return r.putAll(map);
   }

   public static R okay() {
     Map<String, Object> body = new HashMap<>();
     return new R(body, HttpStatus.OK);
   }

   public R put(String key, Object value) {
     Objects.requireNonNull(getBody()).put(key, value);
     return this;
   }

   public R putAll(Map<String, Object> map) {
     Objects.requireNonNull(getBody()).putAll(map);
     return this;
   }

   public boolean notOkay() {
     Integer errcode = (Integer) Objects.requireNonNull(getBody()).get("errcode");
     return (errcode == null || !errcode.equals(0));
   }
 }
